<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class Projectmanager
{
    /** @var object project */
    public $project;
    /** @var mixed rounds */
    public $rounds;
    /** @var mixed matches */
    public $matches;
    /** @var mixed teams */
    public $teams;
    /** @var int group_id */
    public $group;

    /**
     * constructor
     * @param int $project_id
     */
    public function __construct(LM_Table_Project $project)
    {
        $this->project = $project;

        $this->rounds = null;
        $this->teams = null;
        $this->matches = array();
    }

    function getMatches($rid = 0, $tid = 0, $mode = 0, $group = "")
    {

        $matchtable = LM::getLMTable("Match");
        $matchtable->filterbyProject($this->project->getID());

        //if($rid == 0){
        //$rid = $this->project->getCurrentRound();
        //}

        if ($rid > 0) {
            $matchtable->filterbyRound($rid);
        }

        if ($tid > 0) {
            $matchtable->filterbyTeam($tid, $mode);
        }

        if (!empty($group)) {
            $matchtable->filterbyGroup($group);
        }

        return $matchtable->getRows();
    }

    function deleteMatch($id)
    {
        global $database;

        $row = LM::getLMTable("Match");

        $queries[] = "DELETE FROM #__leagueman_event WHERE event_match = " . $database->Quote(intval($id));
        $queries[] = "DELETE FROM #__leagueman_lineup WHERE lineup_match = " . $database->Quote(intval($id));

        $error = false;
        foreach ($queries AS $query) {
            $database->setQuery($query);

            if (!$database->query()) {
                $error = true;
                break;
            }
        }

        if (!$error && $row->delete($id)) {
            return true;
        }

        return false;
    }

    function createMatch($home, $away, $rid, $date = "", $time = "", $group = "")
    {

        $row = LM::getLMTable("Match");
        $row->match_team1 = $home;
        $row->match_team2 = $away;
        $row->match_project = $this->project->getID();
        $row->match_round = $rid;
        $row->match_date = isDate($date) ? $date : "0000-00-00";
        $row->match_time = isTime($time) ? $time : "00:00";

        if ($this->project->getParam("groups_supported")) {
            $row->match_group = $group;
        }

        $row->published = 0;
        return $row->store();
    }

    public function getRounds()
    {
        global $database;

        $sql = "SELECT * FROM #__leagueman_round
				 WHERE round_project = " . $this->project->getID() . " 
			  ORDER BY ordering";
        $database->setQuery($sql);
        return $database->loadObjectList();
    }

    public function getRoundBox()
    {
        $rounds = array();
        foreach ($this->getRounds() AS $round) {
            if ($this->project->getCurrentRound() == $round->ordering) {
                $rounds[] = mosHTML::makeOption($round->ordering, $round->round_name . " (current)");
            } else {
                $rounds[] = mosHTML::makeOption($round->ordering, $round->round_name);
            }
        }
        return $rounds;
    }

    /**
     * function getMatchesbyTeam
     * @param int team_id
     * @param int mode 0 = all, 1 = home, 2 = away 
     * @return mixed 
     */
    public function getMatchesByTeam($team_id, $mode = 0)
    {
        $matchtable = LM::getLMTable("Match");
        $matchtable->filterbyProject($this->project->getID());
        $matchtable->filterbyTeam($team_id, $mode);

        return $matchtable->getRows();
    }

    /**
     * function getMatchesbyRound
     * @param int ordering
     * @return mixed
     */
    public function getMatchesByRound($ordering)
    {
        $matchtable = LM::getLMTable("Match");
        $matchtable->filterbyProject($this->project->getID());
        $matchtable->filterbyRound($this->getRoundbyOrdering($ordering));

        return $matchtable->getRows();
    }

    private function getRoundbyOrdering($odering)
    {
        $rounds = $this->getRounds();
        foreach ($rounds AS $round) {
            if ($round->ordering == $ordering) {
                return $ordering;
            }
        }

        return 0;
    }

}